const crypto = require('crypto')
let vertoken = require("../utils/token")
const { Query } = require('../utils/query')
const { RespError, RespSuccess, RespData } = require('../utils/resp')
const { AuthErrStatus, CommonErrStatus } = require('../utils/error')
const moment = require("moment")
class classify {
  // 增加
  async classifyAdd (ctx, next) {
    let { nickName } = ctx.body
    let { body } = ctx.request
    try {
      // 构建新增SQL语句
      // 插入产品库表(product_classify)中
      const sql_set_classify = `INSERT INTO product_classify SET ?`
      // 入参
      let classifyParams = {
        ...body,
        createBy: nickName,
        createTime: moment().format("YYYY-MM-DD HH:mm:ss"),
        updateTime: moment().format("YYYY-MM-DD HH:mm:ss"),
        updateBy: nickName,
      }
      // 执行新增
      const result = await Query(sql_set_classify, classifyParams)
      let data = {
        rows: result,
        total: 0
      }
      return RespData(ctx, data)
    } catch {
      return RespError(ctx, CommonErrStatus.SERVER_ERR)
    }
  }
  // 删除
  async classifyDelete (ctx, next) {
    let { ids } = ctx.params
    try {
      // 删除文章分类
      let str = `'${ids.replace(/,/g, "','")}'`
      // 构建删除SQL语句
      let delSql = `DELETE FROM product_classify WHERE classify_id IN(${ids});`
      // 执行查询
      let result = await Query(delSql, [])
      let data = {
        massage: '删除成功',
        total: 0
      }
      return RespData(ctx, data)
    } catch {
      return RespError(ctx, CommonErrStatus.SERVER_ERR)
    }
  }
  // 修改
  async classifyEdit (ctx, next) {
    let { body } = ctx.request
    let { nickName } = ctx.body
    try {
      const sql_set_classify = `UPDATE jwzs_database.product_classify SET ? WHERE classify_id = ${body.classifyId}`
      // 入参
      delete body.classifyId
      let classifyParams = {
        ...body,
        updateTime: moment().format("YYYY-MM-DD HH:mm:ss"),
        updateBy: nickName,
      }
      // 执行修改
      const result = await Query(sql_set_classify, classifyParams)
      const data = {
        rows: result,
        total: 0
      }
      return RespData(ctx, data)
    } catch {
      return RespError(ctx, CommonErrStatus.SERVER_ERR)
    }
  }
  // 查询
  // 产品分类列表
  async classifyList (ctx, next) {
    let { pageNum, pageSize, status, title } = ctx.request.query
    const offset = (pageNum - 1) * pageSize // 偏移量
    const wherTitle = `WHERE product_classify.title LIKE '%${title}%'`
    const statusQuery = status ? `AND product_classify.status = ${status}` : ''
    try {
      // 构建查询SQL语句
      const countSql = `SELECT COUNT(*) AS total FROM product_classify ${wherTitle} ${statusQuery};`
      const listSql = `SELECT * FROM product_classify ${wherTitle} ${statusQuery} ORDER BY product_classify.create_time DESC LIMIT ${offset}, ${pageSize};`
      // 执行查询
      let totalResult = (await Query(countSql))[0].total
      let listResult = await Query(listSql, [offset, pageSize])
      let data = {
        rows: listResult,
        total: totalResult
      }
      return RespData(ctx, data)
    } catch {
      return RespError(ctx, CommonErrStatus.SERVER_ERR)
    }
  }
}

module.exports = new classify()